let subscribe = {
	data() {
		return {

		}
	},

	methods: {

		setWithSubscriptions() {
			let self = this

			uni.getSetting({
				withSubscriptions: true,
				success(res) {
					const setting = res.subscriptionsSetting;

					// 1. 用户关闭了总开关
					if (!setting.mainSwitch) {
						self.showOpenSettingGuide();
						return;
					}

					// 2. 用户已设置"总是保持选择"
					if (setting.itemSettings) {
						self.handlePersistedSettings(setting.itemSettings);
						return;
					}

					// 3. 首次请求授权
					self.requestSubscription();


				},
				fail(err) {
					console.error('获取设置失败:', err);
					uni.showToast({
						title: '获取设置失败',
						icon: 'none'
					});
				}
			})
		},
		// 引导用户打开设置
		showOpenSettingGuide() {
			uni.showModal({
				title: '消息订阅未开启',
				content: '需要开启消息订阅才能接收重要通知',
				confirmText: '去开启',
				success: (res) => {
					if (res.confirm) {
						uni.openSetting({
							withSubscriptions: true
						});
					}
				}
			})
		},
		// 处理持久化设置
		handlePersistedSettings(itemSettings) {
			const allAccepted = this.TEMPLATE_IDS.every(id =>
				itemSettings[id] === 'accept'
			);

			const anyRejected = this.TEMPLATE_IDS.some(id =>
				itemSettings[id] === 'reject' || itemSettings[id] === 'ban'
			);

			// 全部接受
			if (allAccepted) {
				this.goPage('/pages/homePages/orderCenter')
				return;
			}

			// 存在拒绝或封禁
			if (anyRejected) {
				const rejectedIds = this.TEMPLATE_IDS.filter(id =>
					itemSettings[id] === 'reject' || itemSettings[id] === 'ban'
				);
				this.goPage('/pages/homePages/orderCenter')
				// uni.showModal({
				// 	title: '订阅被拒绝',
				// 	content: `您已拒绝${rejectedIds.length}个消息模板`,
				// 	confirmText: '重新授权',
				// 	success: (res) => {
				// 		if (res.confirm) this.requestSubscription();
				// 	},

				// 	complete: () => {
				// 		this.goPage('/pages/homePages/orderCenter')
				// 	}
				// });
			}
		},
		// 请求订阅授权
		requestSubscription() {
			uni.requestSubscribeMessage({
				tmplIds: this.TEMPLATE_IDS,
				success: (res) => {
					const results = this.TEMPLATE_IDS.map(id => ({
						id,
						status: res[id] || 'unknown'
					}));


					// 处理授权结果
					const acceptedCount = results.filter(r => r.status === 'accept').length;

					if (acceptedCount === this.TEMPLATE_IDS.length) {
						uni.showToast({
							title: '订阅成功'
						});
					} else if (acceptedCount > 0) {
						uni.showToast({
							title: `成功订阅${acceptedCount}个消息`,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: '订阅被拒绝',
							icon: 'none'
						});
					}
					this.goPage('/pages/homePages/orderCenter')
				},
				fail: (err) => {
					console.error('请求订阅失败:', err);

					// 根据错误码提供具体反馈
					const errorMsg = this.getSubscribeErrorMsg(err.errCode);
					uni.showToast({
						title: errorMsg,
						icon: 'none'
					});
					this.goPage('/pages/homePages/orderCenter')
				}
			});
		},
		// 获取订阅错误信息
		getSubscribeErrorMsg(code) {
			const errors = {
				10001: '参数错误',
				10002: '网络错误',
				20001: '用户拒绝授权',
				20004: '无法执行操作',
				20005: '开发者账号未绑定',
				20006: '无相关权限'
			};
			return errors[code] || `订阅失败(错误码: ${code})`;
		}
	}
}

export default subscribe;